﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace SQL_Manager.OTServ
{
    sealed public class Group
    {
        private Hashtable _Data;
        private Database Db;

        #region Constructors

        //
        // Constructor for a new group
        //
        public Group()
        {
            this._Data = new Hashtable();
            this.name = string.Empty;
            this.flags = 0;
            this.access = 0;
            this.maxdepotitems = 0;
            this.maxviplist = 0;
        }

        //
        // Get group by ID
        //
        public Group(int Group)
        {
            Database.GetInstance(out this.Db);
            this.__retrieve(Group);
        }

        //
        // Get group by ID
        // - Assigns group by reference
        //
        public Group(int Group, out Group group)
        {
            Database.GetInstance(out this.Db);
            this.__retrieve(Group);
            group = this;
        }

        #endregion

        #region Database-Related

        //
        // This saves every field, including custom ones.. since we did load them all
        // - This returns the # of rows affected; 0 is NO ROWS, which means it failed.
        //
        public int commit()
        {
            if (this._Data["id"] != null)
            {
                StringBuilder gstring = new StringBuilder("UPDATE groups SET ");

                foreach (System.Collections.DictionaryEntry entry in this._Data)
                {
                    gstring.AppendFormat("{0}='{1}',", entry.Key, entry.Value);
                }

                gstring.Remove(gstring.Length - 1, 1);
                gstring.AppendFormat(" WHERE id = {0}", this._Data["id"]);

                return this.Db.execute(gstring.ToString());
            }
            else
            {
                StringBuilder fields = new StringBuilder("INSERT INTO groups (");
                StringBuilder values = new StringBuilder(" VALUES(");

                foreach (System.Collections.DictionaryEntry entry in this._Data)
                {
                    fields.AppendFormat("{0},", entry.Key);
                    values.AppendFormat("'{0}',", entry.Value);
                }

                fields.Remove(fields.Length - 1, 1);
                values.Remove(values.Length - 1, 1);
                fields.AppendFormat(") {0})", values.ToString());

                return this.Db.execute(fields.ToString());
            }
        }

        //
        // This loads the group by ID
        //
        private void __retrieve(int group)
        {
            this.Db.query(string.Format("SELECT * FROM groups WHERE id = {0}", group), out this._Data);

            if (this._Data.Count == 0)
            {
                throw new Exceptions.GroupNotFoundException();
            }
        }

        #endregion

        #region Properties

        public int id
        {
            get
            {
                return Convert.ToInt32(this._Data["id"]);
            }
            set
            {
                this._Data["id"] = value;
            }
        }

        public string name
        {
            get
            {
                return (string)this._Data["name"];
            }
            set
            {
                this._Data["name"] = value;
            }
        }

        public int flags
        {
            get
            {
                return Convert.ToInt32(this._Data["flags"]);
            }
            set
            {
                this._Data["flags"] = value;
            }
        }

        public int access
        {
            get
            {
                return Convert.ToInt32(this._Data["access"]);
            }
            set
            {
                this._Data["access"] = value;
            }
        }

        public int maxdepotitems
        {
            get
            {
                return Convert.ToInt32(this._Data["maxdepotitems"]);
            }
            set
            {
                this._Data["maxdepotitems"] = value;
            }
        }

        public int maxviplist
        {
            get
            {
                return Convert.ToInt32(this._Data["maxviplist"]);
            }
            set
            {
                this._Data["maxviplist"] = value;
            }
        }

        #endregion
    }
}